package com.sky.service.impl;

import com.sky.service.ReportService;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private WorkspaceService workspaceService;

    /**
     * 导入报表
     * @param response
     */
    @Override
    public void exportBusinessData(HttpServletResponse response) {
        //1.查询数据库获取营业数据
            //查询最近30天（营业额,订单完成率,新增用户数,有效订单,平均客单价）
        LocalDate beginTimeDate = LocalDate.now().minusDays(30);
        LocalDate endTimeDate = LocalDate.now().minusDays(1);

        LocalDateTime beginTime = LocalDateTime.of(beginTimeDate, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(endTimeDate, LocalTime.MAX);
        //概览数据
        BusinessDataVO businessDataVO = workspaceService.getBusinessData(beginTime,endTime);
        //2.将数据写入到 Excel 文件中
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");

        try{
            //基于模板文件创建新的文件
            XSSFWorkbook excel = new XSSFWorkbook(in);
            //获取表格文件sheet标签页
            XSSFSheet sheet1 = excel.getSheet("Sheet1");
            //填充数据--时间
            sheet1.getRow(1).getCell(1).setCellValue("时间: " + beginTime + " 至 " + endTime);
            sheet1.getRow(3).getCell(2).setCellValue(businessDataVO.getTurnover());
            sheet1.getRow(3).getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());
            sheet1.getRow(3).getCell(6).setCellValue(businessDataVO.getNewUsers());
            sheet1.getRow(4).getCell(2).setCellValue(businessDataVO.getValidOrderCount());
            sheet1.getRow(4).getCell(4).setCellValue(businessDataVO.getUnitPrice());

            //明细数据

            for (int i = 0;i < 30; i++){
                LocalDate date = beginTimeDate.plusDays(i);
                //查询某天营业数据
                BusinessDataVO businessData = workspaceService.getBusinessData(beginTime, LocalDateTime.of(date, LocalTime.MAX));
                sheet1.getRow(7+i).getCell(1).setCellValue(date.toString());
                sheet1.getRow(7+i).getCell(2).setCellValue(businessData.getTurnover());
                sheet1.getRow(7+i).getCell(3).setCellValue(businessData.getOrderCompletionRate());
                sheet1.getRow(7+i).getCell(3).setCellValue(businessData.getValidOrderCount());
                sheet1.getRow(7+i).getCell(3).setCellValue(businessData.getUnitPrice());
                sheet1.getRow(7+i).getCell(3).setCellValue(businessData.getNewUsers());
            }

            //3.通过输出流下载到客户端浏览器
            ServletOutputStream out = response.getOutputStream();
            excel.write(out);

            //关闭资源
            out.close();
            excel.close();
            in.close();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}
